<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="default" @click="handlePrint()">
          <Icon icon="simple-line-icons:printer" /> {{ t('打印') }}
        </a-button> -->
        <a-button type="primary" @click="handleForm({})" v-auth="'bas:wms:box:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasWmsBoxList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { bas3dwhBoxDelete, bas3dwhBoxListData } from '/@/api/bas/wms/box/bas3dwhBox';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { treeData } from '/@/api/bas/wms/area/bas3dwhArea';
  import InputForm from './form.vue';
  import { useModal } from '/@/components/Modal';
  import PrintModal from '/@/components/print/printModal.vue';

  const { t } = useI18n('bas.wms.box.bas3dwhBox');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('箱子管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('箱子编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('所在区域'),
        field: 'areaCode',
        component: 'TreeSelect',
        componentProps: {
          api: treeData,
          // params: { isLoadUser: true, userIdPrefix: '' },
          // canSelectParent: false,
          allowClear: true,
        },
      },

      {
        label: t('装箱状态'),
        field: 'cstatus',
        component: 'Select',
        componentProps: {
          dictType: 'bas_wms_3dwh_box_status',
          allowClear: true,
        },
      },
      {
        label: t('使用状态'),
        field: 'ustatus',
        component: 'Select',
        componentProps: {
          dictType: 'bas_wms_3dwh_box_use_status',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('箱子编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('立体仓区域'),
      dataIndex: 'areaCode',
      key: 'a.area_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('装箱状态'),
      dataIndex: 'cstatus',
      key: 'a.cstatus',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'bas_wms_3dwh_box_status',
    },
    {
      title: t('使用状态'),
      dataIndex: 'ustatus',
      key: 'a.ustatus',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'bas_wms_3dwh_box_use_status',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑箱子'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'bas:wms:box:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除箱子'),
        popConfirm: {
          title: t('是否确认删除箱子'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'bas:wms:box:edit',
      },
    ],
  };

  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getSelectRows }] = useTable({
    api: bas3dwhBoxListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    pagination: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await bas3dwhBoxDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
  // 打印
  const handlePrint = () => {
    let rows = getSelectRows();
    if (rows.length <= 0) {
      showMessage('请至少选择一条要打印的数据!');
    } else {
      let ids = '';
      rows.map((item) => {
        if (ids == '') {
          ids = item.id;
        } else {
          ids = ids + ',' + item.id;
        }
      });
      let obj = {
        title: '立体仓箱码打印',
        ids: ids,
        height: '0.9',
        width: '0.9',
        fileName: '立体仓箱码打印',
      };
      openPrintModal(true, obj);
    }
  };
</script>
